﻿using MobileBSC_Data;
using System;
using System.Collections.Generic;
using System.Data.SQLite;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MobileBSC_SQLite
{
    public class GoalValueMapReader
    {
        private StrategicGoalReader GoalReader { get; set; }
        private KPIReader KPIReader { get; set; }

        private const string SELECT_STMT =
            "SELECT " +
                 "GoalValueMapID " +
               ", StrategicGoalID " +
               ", KPIID " +
               ", Weight " +
            "FROM GoalValueMap ";

        public GoalValueMapReader()
        {
            GoalReader = new StrategicGoalReader();
            KPIReader = new KPIReader();
        }
        public GoalValueMap ReadByID(long id)
        {
            var statement = SELECT_STMT + "WHERE GoalValueMapID = @id;";
            var parameters = new List<SQLiteParameter>();
            parameters.Add(new SQLiteParameter("@id", id));

            return DbTool<GoalValueMap>.ReadSingle(statement, parameters, CreateGVM);
        }
        public List<GoalValueMap> ReadByGoal(long goalId)
        {
            var statement = SELECT_STMT + "WHERE StrategicGoalID = @id;";
            var parameters = new List<SQLiteParameter>();
            parameters.Add(new SQLiteParameter("@id", goalId));

            return DbTool<GoalValueMap>.ReadMultiple(statement, parameters, CreateGVM);
        }
        private GoalValueMap CreateGVM(SQLiteDataReader rd)
        {
            GoalValueMap result = new GoalValueMap();
            result.GoalValueMapID = (long)rd["GoalValueMapID"];
            result.StrategicGoal = GoalReader.ReadByID((long)rd["StrategicGoalID"]);
            result.KPI = KPIReader.ReadByID((long)rd["KPIID"]);
            result.Weight = (decimal)rd["Weight"];

            return result;
        }
    }
}
